package com.pioneer.swing.controller;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.util.Vector;

/**
 * Title:        Pioneer Metals Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class TableRowAdder implements KeyListener {

	private JTable table;

    public TableRowAdder(JTable table) {
		this.table=table;
    }
    public void keyTyped(KeyEvent e) {

    }
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode()==KeyEvent.VK_TAB) {
			int curRow=table.getSelectedRow();
			int curCol=table.getSelectedColumn();
			int maxRows=table.getRowCount();
			int maxCols=table.getColumnCount();
			//System.out.println("Table coordinates are "+curCol+" by "+curRow);
			//System.out.println("Table maxes are "+maxCols+" by "+maxRows);
			if ((maxRows==curRow+1)&&(maxCols==curCol+1)) {
				//System.out.println("Adding new row");
				((DefaultTableModel)table.getModel()).addRow(new Vector());
			}
        }
		//else System.out.println("Key char pressed was "+e.getKeyChar()+" and int was "+e.getKeyCode());
    }

    public void keyReleased(KeyEvent e) {
        /**@todo: Implement this java.awt.event.KeyListener method*/
        //throw new java.lang.UnsupportedOperationException("Method keyReleased() not yet implemented.");
    }
}